package com.qq.supplier.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qq.supplier.entity.Part;
import com.qq.supplier.entity.PartIn;
import com.qq.supplier.service.PartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("part")
public class PartController {

    @Autowired
    PartService partService;

    //进货页面
    @RequestMapping("/partShow")
    String queryAll(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Part> parts = partService.queryAll();
        PageInfo<Part> partPageInfo = new PageInfo<>(parts);
        model.addAttribute("PageInfo", partPageInfo);
        return "supplier/part/showpart";
    }

    @RequestMapping("/addPag")
    String add() {
        return "supplier/part/add";
    }

    @RequestMapping("/add")
    String addpart(Part part) {
        PartIn temp = partService.select(part);//将值传进去进行查找 如果字段内容一致  则不为空
        int count;
        if (temp != null) {
            part.setPartInDate(new Date());
            count = temp.getPartTotals();  //获取到主页面的库存数量
            count += part.getPartTotal();
            temp.setPartTotals(count);
            partService.addPart(part);
            temp.setPartInDates(new Date());
            partService.editPartIn(temp);
            return "redirect:partSystem";
        }else {
            part.setPartInDate(new Date());
            partService.addPart(part);
            partService.addPartIn(part);
            return "redirect:partSystem";

        }
    }

    @RequestMapping("/editPag/{id}")
    String edit(@PathVariable String id, Model model) {
        model.addAttribute("parts", partService.queryOne(id));
        return "supplier/part/edit";
    }

    @RequestMapping("/edit")
    String editPartIn(PartIn partIn) {
        partIn.setPartInDates(new Date());
        partService.editPartIn(partIn);
        return "redirect:partShow";
    }

    //配件库存页面
    @RequestMapping("/partSystem")
    String find(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<PartIn> partList = partService.finds();
        PageInfo<PartIn> pageInfo = new PageInfo<>(partList);
        model.addAttribute("pageInfo1", pageInfo);
        return "supplier/part/partSystem";
    }

  /*  //配件详情页面
    @RequestMapping("/select")
    String selectPart(Model model, @RequestParam("brands") String brands, @RequestParam("types") String types) {
        model.addAttribute("shows", partService.selectPart(brands, types));
        return "supplier/part/xiangxi";
    }*/

    //配件一年内的进货详细信息
    @RequestMapping("selectParts")
    String selectParts(Model model) {
        List<Part> parts = partService.queryAll();
        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        int sum4 = 0;
        int sum5 = 0;
        int sum6 = 0;
        int sum7 = 0;
        int sum8 = 0;
        int sum9 = 0;
        int sum10 = 0;
        int sum11 = 0;
        int sum12 = 0;
        for (int i = 0; i < parts.size(); i++) {
            //判断季度
            Date date = parts.get(i).getPartInDate();
            //创建Calendar对象
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int month = calendar.get(calendar.MONTH);
            int quarter = 0;
            if (month >= 1 && month <= 3) {
                quarter = 1;
            } else if (month >= 4 && month <= 6) {
                quarter = 2;
            } else if (month >= 7 && month <= 9) {
                quarter = 3;
            } else {
                quarter = 4;
            }

            //每个季度的配件订单---配件入库统计图
            if (quarter == 1) {
                if (parts.get(i).getPartVar().equals("日常")) {
                    sum1 += parts.get(i).getPartTotal();
                } else if (parts.get(i).getPartVar().equals("服装")) {
                    sum2 += parts.get(i).getPartTotal();
                } else if (parts.get(i).getPartVar().equals("电器")) {
                    sum3 += parts.get(i).getPartTotal();
                }
            } else if (quarter == 2) {
                if (parts.get(i).getPartVar().equals("日常")) {
                    sum4 += parts.get(i).getPartTotal();
                } else if (parts.get(i).getPartVar().equals("服装")) {
                    sum5 += parts.get(i).getPartTotal();
                } else if (parts.get(i).getPartVar().equals("电器")) {
                    sum6 += parts.get(i).getPartTotal();
                }
            } else if (quarter == 3) {
                if (parts.get(i).getPartVar().equals("日常")) {
                    sum7 += parts.get(i).getPartTotal();
                } else if (parts.get(i).getPartVar().equals("服装")) {
                    sum8 += parts.get(i).getPartTotal();
                } else if (parts.get(i).getPartVar().equals("电器")) {
                    sum9 += parts.get(i).getPartTotal();
                }
            } else if (quarter == 4) {
                if (parts.get(i).getPartVar().equals("日常")) {
                    sum10 += parts.get(i).getPartTotal();
                } else if (parts.get(i).getPartVar().equals("服装")) {
                    sum11 += parts.get(i).getPartTotal();
                } else if (parts.get(i).getPartVar().equals("电器")) {
                    sum12 += parts.get(i).getPartTotal();
                }
            }
        }
        model.addAttribute("sum1", sum1);
        model.addAttribute("sum2", sum2);
        model.addAttribute("sum3", sum3);
        model.addAttribute("sum4", sum4);
        model.addAttribute("sum5", sum5);
        model.addAttribute("sum6", sum6);
        model.addAttribute("sum7", sum7);
        model.addAttribute("sum8", sum8);
        model.addAttribute("sum9", sum9);
        model.addAttribute("sum10", sum10);
        model.addAttribute("sum11", sum11);
        model.addAttribute("sum12", sum12);
        return "supplier/part/selectPart";
    }


}
